<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜系编辑</title>
    <link rel="stylesheet" href="../layui/css/layui.css"/>
    <script type="text/javascript" src="../layui/layui.js"></script>
</head>
<body>
<div class="container">
    <form class="layui-form" lay-filter="formTest" id="updateForm">
        <div class="layui-form-item" style="margin-top: 10px">
            <label class="layui-form-label">
                id
            </label>
            <div class="layui-input-inline">
                <input type="text" id="foodclass_id" name="id"
                       disabled autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                菜系
            </label>
            <div class="layui-input-inline">
                <input type="text" id="foodclass" name="foodclass" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" key="set-mine" lay-filter="save" lay-submit>
                保存
            </button>
        </div>
    </form>
</div>
<script>

    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var id = GetQueryString("classId");
        $.ajax({
            url: "../findFoodClassById.do",
            data: {"classId": id},
            success: function (data) {
                form.val("formTest", {
                    "id": data.classId // "name": "value"
                    , "foodclass": data.className
                })
            },
            dataType: "json",
            error: function () {
                layer.alert("系统出错");
            }
        })
        form.on('submit(save)', function (data) {
            var foodId = data.field.id;//表格数据
            var className = data.field.foodclass;//表格数据
            $.post("../updateFoodClass.do", {"foodId": foodId, "className": className}, function (data) {
                if (data.success) {
                    layer.alert(data.message, {icon: 6}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                        parent.layui.table.reload('demo');
                    });
                } else {
                    layer.alert(data.message, {icon: 3}, function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                }
            }, "json");

            return false;
        });


    });

    //定义此方法可以获取到URL路径上传值
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");//获取参数值的正则表达式
        var urlKeys = window.location.search.substr(1);//获取url传参中？后面的值：id=123&name=小明
        urlKeys = decodeURI(urlKeys);//针对中文乱码的解决方案；decodeURI()解决乱码问题
        var r = urlKeys.match(reg);
        if (r != null) {
            return r[2];
        } else {
            return null;
        }
    }
</script>
</body>
</html>